package com.freeter.modules.controller;


import com.freeter.modules.gjs.entity.UsersSites;
import com.freeter.modules.gjs.service.UsersSitesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users-sites")
@CrossOrigin(origins = "*")
public class UsersSitesController {

    @Autowired
    private UsersSitesService usersSitesService;

    @GetMapping
    public ResponseEntity<List<UsersSites>> getAllUsersSites() {
        List<UsersSites> usersSites = usersSitesService.getAllUsersSites();
        return ResponseEntity.ok(usersSites);
    }

    @GetMapping("/{id}")
    public ResponseEntity<UsersSites> getUsersSitesById(@PathVariable Long id) {
        UsersSites usersSites = usersSitesService.getUsersSitesById(id);
        if (usersSites != null) {
            return ResponseEntity.ok(usersSites);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<UsersSites>> getUsersSitesByUserId(@PathVariable Long userId) {
        List<UsersSites> usersSites = usersSitesService.getUsersSitesByUserId(userId);
        return ResponseEntity.ok(usersSites);
    }

    @GetMapping("/site/{siteCode}")
    public ResponseEntity<List<UsersSites>> getUsersSitesBySiteCode(@PathVariable String siteCode) {
        List<UsersSites> usersSites = usersSitesService.getUsersSitesBySiteCode(siteCode);
        return ResponseEntity.ok(usersSites);
    }

    @GetMapping("/user/{userId}/site/{siteCode}")
    public ResponseEntity<UsersSites> getUsersSitesByUserAndSite(
            @PathVariable Long userId, @PathVariable String siteCode) {
        UsersSites usersSites = usersSitesService.getUsersSitesByUserAndSite(userId, siteCode);
        if (usersSites != null) {
            return ResponseEntity.ok(usersSites);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<UsersSites> createUsersSites(@RequestBody UsersSites usersSites) {
        UsersSites createdUsersSites = usersSitesService.createUsersSites(usersSites);
        return ResponseEntity.ok(createdUsersSites);
    }

    @PutMapping("/{id}")
    public ResponseEntity<UsersSites> updateUsersSites(@PathVariable Long id, @RequestBody UsersSites usersSites) {
        try {
            UsersSites updatedUsersSites = usersSitesService.updateUsersSites(id, usersSites);
            return ResponseEntity.ok(updatedUsersSites);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUsersSites(@PathVariable Long id) {
        usersSitesService.deleteUsersSites(id);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/user/{userId}/site/{siteCode}")
    public ResponseEntity<Void> deleteUsersSitesByUserAndSite(
            @PathVariable Long userId, @PathVariable String siteCode) {
        usersSitesService.deleteUsersSitesByUserAndSite(userId, siteCode);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/status/{status}")
    public ResponseEntity<List<UsersSites>> getUsersSitesByStatus(@PathVariable String status) {
        List<UsersSites> usersSites = usersSitesService.getUsersSitesByStatus(status);
        return ResponseEntity.ok(usersSites);
    }
}